package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.net.GameProtocol;
import com.handinfo.game.MailItem;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.ArrayList;
import com.handinfo.util.Tools;

public class UIMailList extends UIAbstractWindow
{
	// private final Image IMAGE_RECT = UIManager.IMAGE_ITEM_OPEN;
	private final Image IMAGE_SEL = UIManager.IMAGE_CHOOSE;
	private final Image IMAGE_UNREAD = UIManager.IMAGE_MAIL_UNREAD;
	private final String ATT_CHOOSE = "选中";
	private final String ATT_TITLE = "标题";
	private final String ATT_TIME = "时间";
	// 常量
	private static final int H_PRE = UITools.H_PRE + 1;
	private short NUM_ROWS_LIST;// 一屏幕显示的最大邮件条数
	private static short NUM_PAGE_DIS = 1;// 一数据页数据占据页数
	private short NUM_MAILS_SERVER;// 每一数据页包含的邮件记录个数

	// Pop Strs
	private final String POP_REVIEW = "查看";
	private final String POP_MOVETOCOL = "移往收藏夹";
	private final String POP_DELETE = "删除";
	private final String POP_FLAG = "标记";
	private final String POP_FLAGALL = "标记全部";
	private final String POP_BACK = "返回";
	private final String POP_COLSELS = "收藏选中";
	private final String POP_DELETESELS = "删除选中";
	private final String POP_UNFLAGSEL = "取消标记";
	private final String POP_UNFLAGALL = "取消全部";
	// Vars
	private String m_title;
	private MailItem[] m_mailItems;
	private int m_index_page;// 当前页码 0-100
	private int m_index_top;
	private short m_index_page_server;
	private int m_index_mail;
	private int m_sum_mails;// 总数目
	private int m_sum_pages;
	private boolean m_isOperLocked;
	private byte m_type;
	//
	private int m_w_date  ;
	private int m_w_choose ;
	private int m_w_title ;

	public UIMailList(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);

		NUM_ROWS_LIST = (short) getNum();
		NUM_MAILS_SERVER = (short) (NUM_ROWS_LIST * NUM_PAGE_DIS);
		initWidth() ;
	}
	
	private void initWidth(){
		m_w_date = AbstractGameScreen.s_font.stringWidth("120302") + (UITools.IW_RECT_CONTENT << 1) ;
		m_w_choose = AbstractGameScreen.s_font.stringWidth(ATT_CHOOSE) +(UITools.IW_RECT_CONTENT<<1) ;
		m_w_title = UITools.W_RECT - (UITools.IW_RECT_CONTENT << 1) - (m_w_date + m_w_choose) ; 
	}

	private static int getNum()
	{
		return (short) ((UITools.H_RECT - (UITools.H_LABEL_STR + UITools.IH_RECTS) - (UITools.H_LABEL - UITools.H_SOFTKEY)) / UIMailList.H_PRE);
	}
	
	public static int getMailsNum(){
		return getNum() * NUM_PAGE_DIS ;
	}

	public static final MailItem getMailItemById(long id)
	{
		UIMailList mailList = (UIMailList) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MAIL_LIST);
		if (mailList == null) {
			return null;
		}
		MailItem[] items = mailList.m_mailItems;
		if (items == null) {
			return null;
		}
		for (int i = 0, len = items.length; i < len; i++) {
			if (items[i].m_id == id) {
				return items[i];
			}
		}
		return null;
	}

	protected void keyLogic()
	{
		if (m_isOperLocked) {
			return;
		}
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			dirUp();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			dirDown();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
			dirLeft();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
			dirRight();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			clickFire();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			actionLeftSoft();
		}
	}

	private void dirUp()
	{
		if (getDisIndex() == 0) {
			dirLeft();
			changMailIndex(getCurrentPageRows() - 1);
		}
		else {
			changMailIndex(m_index_mail - 1);
		}
	}

	private void dirDown()
	{
		if (getDisIndex() == getCurrentPageRows() - 1) {
			dirRight();
			changMailIndex(m_index_top);
		}
		else {
			changMailIndex(m_index_mail + 1);
		}
	}

	private void dirLeft()
	{
		if (m_mailItems == null) {
			return ;
		}
		if (getDisPageIndex() == 0) {// 显示页最前
			if (m_index_page_server > 0) {// 数据页最前
				m_index_page_server--;
				sendMailListRequest();
			}
		}
		else {
			m_index_page--;
			if (m_index_page_server * NUM_PAGE_DIS == m_index_page) {
				sendMailListRequest();
				return ;
			}
			//
			int disIndex = getDisIndex();
			m_index_top = getDisPageIndex() * NUM_ROWS_LIST;
			changMailIndex(disIndex + m_index_top);
		}
	}

	private void dirRight()
	{
		if (m_mailItems == null) {
			return ;
		}
		if (getDisPageIndex() == getDisSumPage() - 1) {
			int serverSumPage = (m_sum_mails + NUM_MAILS_SERVER - 1) / NUM_MAILS_SERVER;
			if (m_index_page_server < serverSumPage - 1) {// 数据页最后
				m_index_page_server++;
				sendMailListRequest();
			}
		}
		else {
			m_index_page++;
			if (m_index_page_server * NUM_PAGE_DIS == m_index_page-1) {
				
				sendMailListRequest();
				return ;
			}
			int disIndex = getDisIndex();
			m_index_top = getDisPageIndex() * NUM_ROWS_LIST;
			if (disIndex + m_index_top >= m_mailItems.length) {
				changMailIndex(m_mailItems.length - 1);
			}
			else {
				changMailIndex(disIndex + m_index_top);
			}
		}
	}

	private void changMailIndex(int index)
	{
		// 如果文字标题超出现实区域则循环显示
		if (m_mailItems == null || m_mailItems.length == 0) {
			m_index_mail = -1;
			return;
		}
		if (index >= 0 && index < m_mailItems.length) {
			m_index_mail = index;
		}
		String temp = m_mailItems[m_index_mail].m_title;
		m_width_title = AbstractGameScreen.s_font.stringWidth(temp) - (m_w_title - (UITools.IW_RECT_CONTENT << 1));
		m_width_title = m_width_title < 0 ? 0 : m_width_title;
		m_relX_title = 0;
	}

	private int getDisPageIndex()
	{
		return m_index_page - m_index_page_server * NUM_PAGE_DIS;
	}

	private int getDisSumPage()
	{
		return NUM_PAGE_DIS;
	}

	private int getCurrentPageRows()
	{
		int disPageIndex = getDisPageIndex();
		int currSumPages = getDisSumPage();
		return disPageIndex == currSumPages - 1 ? m_mailItems.length - (currSumPages - 1) * NUM_ROWS_LIST
				: NUM_ROWS_LIST;
	}

	private void clickFire()
	{
		if (m_mailItems == null || m_mailItems.length <= 0) {
			return;
		}
		if (m_index_mail >= m_mailItems.length) {
			Tools.debugPrintln("Err Click 1");
			return;
		}
		if (m_mailItems[m_index_mail] == null) {
			Tools.debugPrintln("Err Click 2");
			return;
		}
		m_mailItems[m_index_mail].m_isSel = !m_mailItems[m_index_mail].m_isSel;
	}

	private int getDisIndex()
	{
		return m_index_mail - m_index_top;
	}

	public void sendMailListRequest()
	{
		Tools.debugPrintln("sendMailList m_index_page_server = " + m_index_page_server);
		GameManager.getInstance().m_game_message.sendMailList(m_type, m_index_page_server, getMailsNum());
		m_isOperLocked = true;
	}
	
	private void actionLeftSoft()
	{
		if (m_mailItems[m_index_mail] == null) {
			return;
		}
		final String[] menus = getClickItems(m_index_mail);
		final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_POPMENU);
		popmenu.setPopmenus(menus);
		popmenu.addListener(new UIListener() {

			public void actionPerformed()
			{
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
								|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					int index = popmenu.getCurrentIndex();
					String temp = menus[index];
					if (temp == POP_REVIEW) {
						actionReview(m_index_mail);
					}
					else if (temp == POP_FLAG) {
						m_mailItems[m_index_mail].m_isSel = true;
					}
					else if (temp == POP_FLAGALL) {
						setCurrentPageSel(true);
					}
					else if (temp == POP_UNFLAGALL) {
						setCurrentPageSel(false);
					}
					else if (temp == POP_UNFLAGSEL) {
						m_mailItems[m_index_mail].m_isSel = false;
					}
					else if (temp == POP_DELETE) {
						actionDeleteSel(m_index_mail);
					}
					else if (temp == POP_DELETESELS) {
						actionDeleteSels(m_index_mail);
					}
					else if (temp == POP_MOVETOCOL) {
						actionColSel(m_index_mail);
					}
					else if (temp == POP_COLSELS) {
						actionColSels(m_index_mail);
					}
					UIManager.getInstance().removeWindow(popmenu);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(popmenu);
				}
			}

			private void actionReview(int index)
			{
				Tools.debugPrintln("Review ");
				MailItem item = m_mailItems[index];
				GameManager.getInstance().m_game_message.sendMailDetail(item.m_id);
				item.m_state = MailItem.STATE_MAIL_READ;
			}

			private void actionColSel(int index)
			{
				Tools.debugPrintln("actionColSel  ");
				sendCol(new long[] { m_mailItems[index].m_id });
			}

			private void actionColSels(int index)
			{
				Tools.debugPrintln("actionColSels ");
				sendCol(getSelIds());
			}

			private void actionDeleteSel(int index)
			{
				Tools.debugPrintln("actionDeleteSel index = " + index);
				sendDelete(new long[] { m_mailItems[index].m_id });
			}

			private void actionDeleteSels(int index)
			{
				Tools.debugPrintln("actionDeleteSels ");
				sendDelete(getSelIds());
			}

			private long[] getSelIds()
			{
				ArrayList list = new ArrayList();
				for (int i = 0; i < NUM_ROWS_LIST; i++) {
					int index = i + m_index_top;
					if (index >= m_mailItems.length) {
						break;
					}
					if (m_mailItems[index] == null) {
						continue;
					}
					if (m_mailItems[index].m_isSel) {
						list.addElement(m_mailItems[i]);
					}
				}
				int size = list.size();
				if (size == 0) {
					return null;
				}
				long[] ids = new long[size];
				for (int i = 0; i < size; i++) {
					MailItem item = (MailItem) list.elementAt(i);
					ids[i] = item.m_id;
				}
				return ids;
			}

			private void setCurrentPageSel(boolean isSel)
			{
				for (int j = 0; j < NUM_ROWS_LIST; j++) {
					int tempIndex = j + m_index_top;
					if (tempIndex >= m_mailItems.length) {
						return;
					}
					if (m_mailItems[tempIndex] == null) {
						continue;
					}
					m_mailItems[tempIndex].m_isSel = isSel;
				}
			}

			private void sendDelete(final long[] ids)
			{
				if (ids == null) {
					return;
				}
				final UIMessageBox messageBox = (UIMessageBox) UIManager.getInstance().createWindow(
						UIWindowFactory.WINDOW_MESSAGEBOX);
				messageBox.setMessage("是否删除邮件?");
				messageBox.addListener(new UIListener() {

					public void actionPerformed()
					{
						if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
								|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
							GameManager.getInstance().m_game_message.sendMailDelete(ids);
							sendMailListRequest();
							m_isOperLocked = true;
							UIManager.getInstance().removeWindow(messageBox);
						}
						else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
							UIManager.getInstance().removeWindow(messageBox);
						}
					}
				});

			}

			private void sendCol(long[] ids)
			{
				if (ids == null) {
					return;
				}
				GameManager.getInstance().m_game_message.sendMailCollectMail(ids);
				sendMailListRequest();
				m_isOperLocked = true;
			}

		});

	}

	private String[] getClickItems(int index)
	{
		String[] menus = null;
		switch (m_type) {
			case MailItem.TYPE_MAIL_NOR:
				if (m_mailItems[m_index_mail].m_isSel) {
					menus = new String[] { POP_REVIEW, POP_COLSELS, POP_DELETESELS, POP_UNFLAGALL, POP_UNFLAGSEL,
							POP_BACK };
				}
				else {
					menus = new String[] { POP_REVIEW, POP_MOVETOCOL, POP_DELETE, POP_FLAGALL, POP_FLAG, POP_BACK };
				}
				break;
			case MailItem.TYPE_MAIL_SYS:
				if (m_mailItems[m_index_mail].m_isSel) {
					menus = new String[] { POP_REVIEW, POP_DELETESELS, POP_UNFLAGALL, POP_UNFLAGSEL, POP_BACK };
				}
				else {
					menus = new String[] { POP_REVIEW, POP_DELETE, POP_FLAGALL, POP_FLAG, POP_BACK };
				}
				break;
			case MailItem.TYPE_MAIL_COL:
				if (m_mailItems[m_index_mail].m_isSel) {
					menus = new String[] { POP_REVIEW, POP_DELETESELS, POP_UNFLAGALL, POP_UNFLAGSEL, POP_BACK };
				}
				else {
					menus = new String[] { POP_REVIEW, POP_DELETE, POP_FLAGALL, POP_FLAG, POP_BACK };
				}
				break;
		}
		return menus;
	}

	/**
	 * 数据发生变化时修改变量
	 */
	private void resetOnDataChanged()
	{
		m_mailItems = removeNull();// 上移
		init();
	}

	private void init()
	{
		m_sum_pages = countSumPages();
		m_index_page = countCurrentPage();
		m_index_top = countTopIndex();
		m_index_mail = countCurrentIndex();
		m_isOperLocked = false;
	}

	private MailItem[] removeNull()
	{
		if (m_mailItems == null || m_mailItems.length == 0) {
			return null;
		}
		ArrayList list = new ArrayList();
		for (int i = 0; i < m_mailItems.length; i++) {
			if (m_mailItems[i] == null) {
				continue;
			}
			list.addElement(m_mailItems[i]);
		}
		int size = list.size();
		if (size == 0) {
			return null;
		}
		m_mailItems = new MailItem[size];
		for (int i = 0; i < size; i++) {
			m_mailItems[i] = (MailItem) list.elementAt(i);
		}
		return m_mailItems;
	}

	/**
	 * 获得总的页数
	 * @return
	 */
	private int countSumPages()
	{
		return (m_sum_mails + NUM_ROWS_LIST - 1) / NUM_ROWS_LIST;
	}

	/**
	 * 计算当前的页码
	 * @return
	 */
	private int countCurrentPage()
	{
		int num = (NUM_MAILS_SERVER + NUM_ROWS_LIST - 1) / NUM_ROWS_LIST;// 一页显示多
		return m_index_page_server * num;
	}

	/**
	 * 获得当前选中的数据索引
	 * @return
	 */
	private int countTopIndex()
	{
		return 0;
	}

	/**
	 * 获得当前选中的数据索引
	 * @return
	 */
	private int countCurrentIndex()
	{
		return 0;
	}

	/***************************/
	// 接收Message
	/***************************/

	/**
	 * 点击邮箱打开邮件menu列表
	 */
	public static final void openMailMenu()
	{
		final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_POPMENU);
		popmenu.setPopmenus(new String[] { "普通邮件", "系统邮件", "收藏夹", "写邮件", "返回" });
		popmenu.setCentre();
		popmenu.addListener(new UIListener() {

			public void actionPerformed()
			{
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					int index = popmenu.getCurrentIndex();
					switch (index) {
						case 0:// 普通邮件列表
							sendMailList(MailItem.TYPE_MAIL_NOR);
							break;
						case 1:// 系统邮件列表
							sendMailList(MailItem.TYPE_MAIL_SYS);
							break;
						case 2:// 收藏夹
							sendMailList(MailItem.TYPE_MAIL_COL);
							break;
						case 3:// 写邮件
							UIMail.openSendMail(null);
							break;
					}
					MailItem.s_num_newMail = 0;
					UIManager.getInstance().removeWindow(popmenu);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(popmenu);
				}
			}

			private void sendMailList(byte mailType)
			{
				GameManager.getInstance().m_game_message.sendMailList(mailType, (short) 0,getMailsNum());
			}
		});
	}

	/**
	 * 获得邮件列表
	 */
	public static final void recvMailList(DataInputStream dis) throws IOException
	{
		UIMailList mailList = (UIMailList) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MAIL_LIST);
		byte type = dis.readByte();
		short pageIndex = dis.readShort();
		int sum_mails = dis.readInt();
		if (sum_mails == 0) {
			if (mailList == null) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "无邮件");
			}else{
				mailList.close();
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "收件箱所有邮件均被删除");
			}
			return;
		}
		int num_mails_cur = dis.readInt();
		if (num_mails_cur == 0) {
			if (mailList == null) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "无邮件");
			}else{
				mailList.close();
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "收件箱所有邮件均被删除");
			}
			return;
		}
		if (mailList == null) {// 第一次获取数据
			mailList = (UIMailList) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MAIL_LIST);
		}
		mailList.clear();
		mailList.changeState(type);
		mailList.m_index_page_server = pageIndex;
		mailList.m_sum_mails = sum_mails;
		if (sum_mails == 0) {
			mailList.m_mailItems = null;
		}
		else {
			mailList.m_mailItems = new MailItem[num_mails_cur];
			for (int i = 0; i < num_mails_cur; i++) {
				MailItem item = new MailItem();
				item.m_id = dis.readLong();
				item.m_state = dis.readByte();
				item.m_title = dis.readUTF();
				item.m_date = MailItem.getTime(dis.readLong());
				mailList.m_mailItems[i] = item;
			}
		}
		mailList.init();
		mailList.changMailIndex(0);
		// 当处于收件箱时候，需删除收件箱
		UIMail mail = (UIMail) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MAIL);
		if (mail != null) {
			UIManager.getInstance().removeWindow(mail);
		}
		
		MailItem.s_num_newMail = 0 ;
	}

	private void clear()
	{
		m_mailItems = null;
	}

	private void changeState(byte type)
	{
		m_type = type;
		switch (m_type) {
			case MailItem.TYPE_MAIL_NOR:
				m_title = "普通邮件";
				break;
			case MailItem.TYPE_MAIL_COL:
				m_title = "收藏夹";
				break;
			case MailItem.TYPE_MAIL_SYS:
			default:
				m_title = "系统邮件";
				break;
		}
	}

	public void recvMessage(int orderId, DataInputStream dis) throws IOException
	{
		switch (orderId) {
			case GameProtocol.S_MAILLIST_DELETE:
			case GameProtocol.S_MAILLIST_COLLECT_MAIL:
				removeItems(dis);
				break;
		}
	}

	private void removeItems(DataInputStream dis) throws IOException
	{
		int num = dis.readInt();
		if (num == 0) {
			close();
			return ;
		}
		for (int i = 0; i < num; i++) {
			byte state = dis.readByte();
			if (state == 0) {
				continue;
			}
			long id = dis.readLong();
			for (int j = 0; j < m_mailItems.length; j++) {
				if (m_mailItems[j] == null) {
					continue;
				}
				if (m_mailItems[j].m_id == id) {
					m_mailItems[j] = null;
					m_sum_mails--;
					break;
				}
			}
		}
		resetOnDataChanged();
	}

	/***************************/
	// 绘制
	/***************************/
	// 
	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, m_title);
		int tempY = paintTop(g);
		paintMailList(g, m_x + UITools.INITX_CONTENT, tempY);
		paintPage(g, m_x + (m_w >> 1), m_y + m_h - UITools.IW_WIN);
	}

	/**
	 * 绘制标题头部
	 * @param g
	 * @return
	 */
	private int paintTop(Graphics g)
	{
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_y + UITools.INITY_CONTENT;
		UITools.paintTitle(g, tempX, tempY, m_w_choose, ATT_CHOOSE);
		tempX += (m_w_choose + UITools.IW_RECT_CONTENT);
		UITools.paintTitle(g, tempX, tempY, m_w_title, ATT_TITLE);
		tempX += (m_w_title + UITools.IW_RECT_CONTENT);
		UITools.paintTitle(g, tempX, tempY, m_w_date, ATT_TIME);
		return tempY + UITools.H_LABEL_STR;
	}

	
	/**
	 * 邮件列表
	 * @param g
	 * @param x
	 * @param y
	 */
	private void paintMailList(Graphics g, int x, int y)
	{
		if (m_mailItems == null) {
			return;
		}
		int tempY = y + UITools.IH_RECT_CONTENT;
		int tempX = x;
		UITools.paintContentRect(g,tempX, tempY, m_w_choose, H_PRE, NUM_ROWS_LIST);
		paintChooseList(g, tempX, tempY);
		tempX += (m_w_choose + UITools.IW_RECT_CONTENT);
		UITools.paintContentRect(g, tempX, tempY, m_w_title, H_PRE, NUM_ROWS_LIST);
		paintTitleList(g, tempX, tempY);
		tempX += (m_w_title + UITools.IW_RECT_CONTENT);
		UITools.paintContentRect(g,  tempX, tempY, m_w_date, H_PRE, NUM_ROWS_LIST);
		paintDateList(g, tempX, tempY);
		//
		tempX = x + UITools.IW_RECT_CONTENT;
		tempY += 2;
		int focusY = tempY;
		for (int i = 0; i < NUM_ROWS_LIST; i++) {
			int index = m_index_top + i;
			if (index >= m_mailItems.length) {
				break;
			}
			if (m_mailItems[index] == null) {
				continue;
			}
			boolean isSel = m_index_mail == index;
			// paintMailItem(g, tempX, tempY, m_mailItems[index], isSel);
			if (isSel) {
				focusY = tempY;
			}
			tempY += H_PRE;
		}
		g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH, AbstractGameScreen.SCREEN_HEIGHT);
		UITools.drawSelectBox(g, tempX, focusY, UITools.W_CONTENT, H_PRE);
	}

	private void paintChooseList(Graphics g, int x, int y)
	{
		int tempY = y + ((H_PRE - UITools.H_BAG) >> 1) + 3;
		int tempX = x + ((m_w_choose - UITools.W_BAG) >> 1);
		for (int i = 0; i < NUM_ROWS_LIST; i++) {
			int index = m_index_top + i;
			if (index >= m_mailItems.length) {
				break;
			}
			if (m_mailItems[index] == null) {
				continue;
			}
			g.drawImage(UIManager.IMAGE_ITEM_OPEN, tempX, tempY, Tools.LEFT_TOP);
			if (m_mailItems[index].m_isSel) {
				g.drawImage(IMAGE_SEL, tempX, tempY + 2, Tools.LEFT_TOP);
			}
			tempY += H_PRE;
		}
	}

	/**
	 * 2012-2-21 下午01:11:15 venizeng
	 * @param g
	 * @param tempX
	 * @param tempY
	 */
	private void paintDateList(Graphics g, int x, int y)
	{
		int tempY = y + 3;
		int tempX = x + UITools.IW_RECT_CONTENT;
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		for (int i = 0; i < NUM_ROWS_LIST; i++) {
			int index = m_index_top + i;
			if (index >= m_mailItems.length) {
				break;
			}
			if (m_mailItems[index] == null) {
				continue;
			}
			g.drawString(m_mailItems[index].m_date, tempX, tempY, 0);
			if (UIManager.TIME % 6 < 3) {
				if (m_mailItems[index].m_state == MailItem.STATE_MAIL_NEW
						|| m_mailItems[index].m_state == MailItem.STATE_MAIL_UNREAD) {
					
					g.drawImage(IMAGE_UNREAD, tempX + AbstractGameScreen.s_font.stringWidth(m_mailItems[index].m_date) + 2,
							tempY + UITools.IH_RECT_CONTENT, Tools.RIGHT_TOP);
				}
			}
			tempY += H_PRE;
		}
	}

	private int m_relX_title;
	private int m_width_title;

	/**
	 * 2012-2-21 下午01:11:13 venizeng
	 * @param g
	 * @param tempX
	 * @param tempY
	 */
	private void paintTitleList(Graphics g, int x, int y)
	{
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		int tempY = y + 3;
		int tempX = x + UITools.IW_RECT_CONTENT;
		int tempW = m_w_title - (UITools.IW_RECT_CONTENT << 1);
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		for (int i = 0; i < NUM_ROWS_LIST; i++) {
			int index = m_index_top + i;
			if (index >= m_mailItems.length) {
				break;
			}
			if (m_mailItems[index] == null) {
				continue;
			}
			g.setClip(tempX, tempY, tempW, H_PRE);
			if (m_index_mail == index) {
				g.drawString(m_mailItems[index].m_title, tempX - m_relX_title, tempY, 0);
				if (m_relX_title == m_width_title) {
					m_relX_title = 0;
				}
				else {
					m_relX_title++;
				}
			}
			else {
				g.drawString(m_mailItems[index].m_title, tempX, tempY, 0);
			}
			tempY += H_PRE;
		}
		g.setClip(clipX, clipY, clipW, clipH);
	}

	private void paintPage(Graphics g, int x, int y)
	{
		UITools.paintPage(g, x, y, m_index_page, m_sum_pages == 0 ? 1 : m_sum_pages);
	}
}
